from tw.forms.validators import Int, NotEmpty,UnicodeString,All,ForEach,DateConverter
from tw.forms.fields import TableForm,TextField, TextArea, CheckBox, HiddenField, RadioButtonList,SingleSelectField
from tw.forms import CalendarDatePicker,validators
from add_metadata import NakedDateConverter
from tw.api import Widget,WidgetsList
from tg import url
import tw.dynforms as twd
from lifepyscle.controllers.common import MAX_RECUR,choice_reminder_days
from custom_validators import DateRange,AllValidEmails,StartBeforeEnd
from tw.jquery.autocomplete import AutoCompleteField
from formencode.schema import Schema
import formencode

class EventSchema(Schema):
    allow_extra_fields = True
    filter_extra_fields = True
    #starts = DateRange()
    ends = UnicodeString()
    title = NotEmpty()
    chained_validators = [StartBeforeEnd('starts', 'ends')]

class EventFormFields(WidgetsList):
    record_id = HiddenField(id="record_id")
    event_id = HiddenField(id="event_id")
    r2r = HiddenField(id='r2r')
    event_type = AutoCompleteField(id='event_type',label_text=u'Type:',\
                attrs=dict(style="width:90%"),\
                completionURL = '/event/fetch_types',\
                fetchJSON = True,\
                minChars = 3,validator=UnicodeString(),\
                help_text=u"Start typing to choose an event type. Available event types will be shown automatically. If you enter a new type it will immediately become available for other events and other users")
    title = TextField(id='title',help_text=u'Give the event a title - e.g. Milestone 1',attrs=dict(style="width:90%"))
    desc = TextArea(id='desc',help_text=u'Give the event a description. This field is indexed to make it easily searchable')
    starts = CalendarDatePicker(label_for=u'starts',label_text=u'starts',id='starts',help_text=u'The date that the event starts',date_format="%Y-%m-%d")
    ends = TextField(label_for=u'ends',label_text=u'ends',id='ends',help_text=u'The date on which the event ends. You can use a date formatted as YYYY-MM-DD, or you can enter "3y" for start date plus 3 years. If you enter nothing, the event will be taken to end on the same date as it starts')
    #reminders = ItemForm() opting to use an jscript pop up to add reminders instead of the stress of validating a growing field

event_form = TableForm(id="event_form",
                    hover_help = True,
                    fields=EventFormFields(),
                    validator=EventSchema,
                    action="/event/add")
